package com.kerrykidz.scminvoicing.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.scminvoicing.dao.mapper.ScmPayformEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmPayformOrderEntityMapper;
import com.kerrykidz.scminvoicing.dao.mapper.ScmPurOrderEntityMapper;
import com.kerrykidz.scminvoicing.dao.model.ScmPayformEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPayformOrderEntity;
import com.kerrykidz.scminvoicing.dao.model.ScmPurOrderEntity;
import com.kerrykidz.scminvoicing.service.IScmPayformService;
import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.FileUtil;
import com.kerrykidz.system.util.JXLSUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.ExportParamVo;
import com.kerrykidz.system.vo.ExportVo;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.service.IWorkflowService;
import com.rdp.framework.exception.RDPException;

/**
 * 
* @ClassName: ScmPayformServiceImpl 
* @description:付款申请单操作的service接口实现类
* @author: chenyu 
* @date: 2014年9月7日 下午5:36:53
* @version: V1.0
*
 */
@Service
public class ScmPayformServiceImpl implements IScmPayformService {
@Autowired
private ScmPayformEntityMapper scmPayformEntityMapper;
@Autowired
private ScmPayformOrderEntityMapper scmPayformOrderEntityMapper;
@Autowired
private ScmPurOrderEntityMapper scmPurOrderEntityMapper;


@Autowired
IWorkflowService workflowService;

	@Override
	public List<ScmPayformEntity> findScmPayformEntityList(
			ScmPayformEntity scmPayform) {
		scmPayform.setIspaging(true);
		return scmPayformEntityMapper.findScmPayformEntityList(scmPayform);
	}

	@Override
	public void addScmPayform(ScmPayformEntity scmPayform) {
		scmPayformEntityMapper.addScmPayform(scmPayform);
	}

	@Override
	public void updateScmPayform(ScmPayformEntity scmPayform) {
		scmPayformEntityMapper.updateScmPayform(scmPayform);
	}

	@Override
	public ScmPayformEntity getScmPayformById(String pkPayformId) {
		return scmPayformEntityMapper.getScmPayformById(pkPayformId);
	}

	@Override
	public List<ScmPurOrderEntity> findScmPurOrderPayForm(
			ScmPurOrderEntity scmPurOrder) throws RDPException {
		return scmPurOrderEntityMapper.findScmPurOrderPayForm(scmPurOrder);
	}

	@Override
	public List<ScmPurOrderEntity> scmPurOrderListByPayFormId(String pkPayFormId)
			throws RDPException {
		return scmPurOrderEntityMapper.scmPurOrderListByPayFormId(pkPayFormId);
	}

	@Override
	public void delScmPayformOrderByTwoId(String fkPayformId, String fkPurordId) throws RDPException {
		scmPayformOrderEntityMapper.delScmPayformOrderByTwoId(fkPayformId, fkPurordId);
		
	}

	@Override
	public void delScmPayformOrderByPayformId(String fkPayformId) {
		scmPayformOrderEntityMapper.delScmPayformOrderByPayformId(fkPayformId);
	}

	@Override
	public ScmPayformOrderEntity getPayFormOrderByTwoId(String fkPayformId,
			String fkPurordId) throws RDPException {
		return scmPayformOrderEntityMapper.getPayFormOrderByTwoId(fkPayformId, fkPurordId);
	}

	@Override
	public void addScmPayFormOrder(ScmPayformOrderEntity scmPayFormOrder) {
		scmPayformOrderEntityMapper.addScmPayFormOrder(scmPayFormOrder);
	}

	@Override
	public void addScmPayFormOrderBatch(
			List<ScmPayformOrderEntity> scmPayFormOrderList) {
		scmPayformOrderEntityMapper.addScmPayFormOrderBatch(scmPayFormOrderList);
		
	}

	@Override
	public List<ScmPayformOrderEntity> getPayFormOrderByFkPayformId(
			String fkPayformId) {
		return scmPayformOrderEntityMapper.getPayFormOrderByFkPayformId(fkPayformId);
	}
	
	@Override
	public List<ScmPayformOrderEntity> getPayFormOrderByPurordId(String fkPurordId)
			throws RDPException {
		return scmPayformOrderEntityMapper.getPayFormOrderByPurordId(fkPurordId);
	}

	@Override
	public List<ScmPurOrderEntity> scmPurOrderListSupIdAndByPayFormId(
			String supplyId, String pkPayFormId) throws RDPException {
		return scmPurOrderEntityMapper.scmPurOrderListSupIdAndByPayFormId(supplyId,pkPayFormId);
	}

	/**
	 * 导出付款明细
	 *
	 * @param: @param vo
	 * @param: @param response
	 * @param: @throws RDPException
	 * @return: ScmPayformServiceImpl
	 * @throws
	 */
	@Override
	public void exportPayDetailed(ExportParamVo vo, HttpServletResponse response)
			throws RDPException {
		UserInfo user = UserUtil.getUserFromSession();
		if (null != vo.getStartTime() && !"".equals(vo.getStartTime())) {
			vo.setStartTime(vo.getStartTime() + " 00:00:00");
		}
		if (null != vo.getEndTime()&& !"".equals(vo.getEndTime())) {
			vo.setEndTime(vo.getEndTime() + " 23:59:59");
		}
		ScmPayformEntity scmPayform = new ScmPayformEntity();
		scmPayform.setPkPayformId(vo.getCode());
		scmPayform.setSupName(vo.getSupName());
		scmPayform.setFkBranId(vo.getBranId());
		scmPayform.setStartApplyTime(vo.getStartTime());
		scmPayform.setEndApplyTime(vo.getEndTime());
		scmPayform.setUserId(user.getUserId());
		List<ScmPayformEntity> payDetailedList =  scmPayformEntityMapper.findScmPayformEntityList(scmPayform);
		if (null != payDetailedList && payDetailedList.size() > 0) {
			for (ScmPayformEntity p : payDetailedList) {
				p.setWorkflowVo(workflowService.getWorkflowVo(
						p.getFkFlowinstId(), p.getStatus(),
						UserUtil.getUserFromSession()));
			}
		}
		ExportVo exportVo = new ExportVo();
		exportVo.setOptTime(DateUtil.formatDate("yyyy.MM.dd", new Date()));
		exportVo.setOptUser(user.getName());
		exportVo.setPayDetailedList(payDetailedList);
		Map<String,Object> beans = new HashMap<String,Object>();
		beans.put("payDetailed", exportVo);
		String destFile = JXLSUtil.exportExcel("payDetailed", beans);
		try {
			FileUtil.downLoadFile(destFile, response);
		} catch (Exception e) {
			throw new RDPException("导出文件失败");
		}
	}

}
